package com.dycong.common.common;

/**
 * Created by dycong on 2017/3/21.
 */
public class StringFormat {
    //占位符%s
    public static String formatString(String src, String... strings) {
        return String.format(src, strings);
    }

    //占位符%c
    public static String formatChar(String src, Character... characters) {
        return String.format(src, characters);
    }

    //占位符%b
    public static String formatBoolean(String src, Boolean... booleans) {
        return String.format(src, booleans);
    }

    //占位符%d,整数
    public static String formatInteger(String src, Integer... integers) {
        return String.format(src, integers);
    }

    //占位符%x，十六进制
    public static String formatHex(String src, Integer... integers) {
        return String.format(src, integers);
    }

    //占位符%x，八进制
    public static String formatOctal(String src, Integer... integers) {
        return String.format(src, integers);
    }

    //占位符%f,浮点类型
    public static String formatFloat(String src, Double... doubles) {
        return String.format(src, doubles);
    }

    //占位符%a,十六进制浮点类型
    public static String formatHexFloat(String src, Double... doubles) {
        return String.format(src, doubles);
    }

    //占位符%e,指数类型
    public static String formatExp(String src, Double... doubles) {
        return String.format(src, doubles);
    }

    //占位符%g,通用浮点类型（f和e类型中较短的）
    public static String formatExpFloatShorter(String src, Double... doubles) {
        return String.format(src, doubles);
    }

    //占位符%h,散列码
    public static String formatHash(String src, Object... objects) {
        return String.format(src, objects);
    }

    //占位符%%, 百分比类型
    public static String formatPercent(String src, Object... objects) {
        return String.format(src, objects);
    }

    /*TODO 更多占位符参见 ： http://blog.csdn.net/lonely_fireworks/article/details/7962171*/
    public static void main(String[] args) {
        System.out.println(formatString("Hi,%s:%s.%s", "王南", "王力", "王张"));
        System.out.println(formatChar("字母a的大写是：%c %n", 'A'));
        System.out.println(formatBoolean("3>7的结果是：%b %n", 3 > 7));
        System.out.println(formatInteger("100的一半是：%d %n", 100 / 2));
        System.out.println(formatHex("100的16进制数是：%#x %n", 100));
        System.out.println(formatOctal("100的8进制数是：%#o %n", 100));
        System.out.println(formatFloat("50元的书打8.5折扣是：%f 元%n", 50 * 0.85));
        System.out.println(formatHexFloat("上面价格的16进制数是：%a %n", 50 * 0.85));
        System.out.println(formatExp("上面价格的指数表示：%e %n", 50 * 0.85));
        System.out.println(formatExpFloatShorter("上面价格的指数和浮点数结果的长度较短的是：%g %n", 50 * 0.85));
        System.out.println(formatHash("字母A的散列码是：%h %n", 'A'));
        System.out.println(formatPercent("上面的折扣是%d%% %n", 85));
        /*数字前部零填充*/
        System.out.println(String.format("%0" + 6 + "d", 1));
    }
}
